/*MyPhotoIndex Free Photo Organizer <www.myphotoindex.com>
  Copyright (C) <2006-2009>  <Asaf Yarkoni asaf@yarkoni.net>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MyPhotoIndex.Controls;
using MyPhotoIndex.Settings;
using System.Globalization;
using System.Collections.ObjectModel;
using MyPhotoIndex.Interfaces.Plugins;
using MyPhotoIndex.Plugins;
using MyPhotoIndex.Utilities;

namespace MyPhotoIndex
{
    public partial class FullScreenToolstrip : AutoFadingForm
    {
        public int ImageZoomPercent
        {
            get
            {
                return Convert.ToInt32(ZoomToolStripLabel.Text.Replace("%", ""), CultureInfo.InvariantCulture);
            }
            set
            {
                ZoomToolStripLabel.Text = value.ToString(CultureInfo.InvariantCulture) + "%";
            }
        }

        public FullScreenToolstrip()
        {
            InitializeComponent();
            Stop.Enabled = false;

            effectToolStripComboBox.MouseHover += new EventHandler(effectToolStripComboBox_MouseHover);
            effectToolStripComboBox.MouseLeave += new EventHandler(effectToolStripComboBox_MouseLeave);
            LoadEffects();
        }

        void effectToolStripComboBox_MouseLeave(object sender, EventArgs e)
        {
            AutoFadingForm_MouseLeave(sender, e);
        }

        void effectToolStripComboBox_MouseHover(object sender, EventArgs e)
        {
            this.Select();
            AutoFadingForm_MouseHover(sender, e);
        }

        private void LoadEffects()
        {
            effectToolStripComboBox.Items.Add(new DefaultSlideShowEffect());
            effectToolStripComboBox.Items.Add(new RandomSlideShowEffect());

            ReadOnlyCollection<ISlideShowEffectPlugin> plugins = Manager.GetPlugins<ISlideShowEffectPlugin>();
            if (plugins.Count > 0)
            {
                foreach (ISlideShowEffectPlugin plugin in plugins)
                {
                    effectToolStripComboBox.Items.Add(plugin);
                }
            }

            string defaultPlugin = ApplicationSettings.Default.DefaultSlideShowEffectPlugin;
            effectToolStripComboBox.SelectedIndex = 0;
            foreach (ISlideShowEffectPlugin plugin in effectToolStripComboBox.Items)
            {
                if (string.Compare(plugin.GetType().FullName, defaultPlugin) == 0)
                {
                    effectToolStripComboBox.SelectedItem = plugin;
                }
            }
        }

        public ISlideShowEffectPlugin Effect
        {
            get { return effectToolStripComboBox.SelectedItem as ISlideShowEffectPlugin; }
        }

        private void FullScreenToolstrip_Shown(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            try
            {
                ApplicationSettings.Default.DefaultSlideShowEffectPlugin = effectToolStripComboBox.SelectedItem.GetType().FullName;
                ApplicationSettings.Default.Save();
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
            }

            base.OnClosing(e);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            base.Fade(true, false);
        }

        private void toolStrip1_MouseLeave(object sender, EventArgs e)
        {
            AutoFadingForm_MouseLeave(sender, e);
        }

        private void toolStrip1_MouseHover(object sender, EventArgs e)
        {
            this.Select();
            AutoFadingForm_MouseHover(sender, e);
        }

        private void toolStrip1_MouseEnter(object sender, EventArgs e)
        {
            AutoFadingForm_MouseEnter(sender, e);
        }

        private void toolStrip1_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    this.Owner.Close();
                break;
            }
        }

        private void Cancel_Click(object sender, EventArgs e)
        {
            FullScreenForm fsf = (FullScreenForm)this.Owner;

            fsf.StopSlideShow();
            fsf.Close();
        }

        private void Stop_Click(object sender, EventArgs e)
        {
            FullScreenForm fsf = (FullScreenForm)this.Owner;
            fsf.StopSlideShow();

            Play.Enabled = true;
            Stop.Enabled = false;
        }

        private void Play_Click(object sender, EventArgs e)
        {
            Play.Enabled = false;
            Stop.Enabled = true;

            FullScreenForm fsf = (FullScreenForm)this.Owner;
            fsf.StartSlideShow(ApplicationSettings.Default.SlideShowTransitionInterval);
        }

        private void ZoomUpToolStripButton_Click(object sender, EventArgs e)
        {
            FullScreenForm fsf = (FullScreenForm)this.Owner;
            fsf.ZoomIn();
        }

        private void ZoomDownToolStripButton_Click(object sender, EventArgs e)
        {
            FullScreenForm fsf = (FullScreenForm)this.Owner;
            fsf.ZoomOut();
        }

        private void ActualSizeToolStripButton_Click(object sender, EventArgs e)
        {
            FullScreenForm fsf = (FullScreenForm)this.Owner;
            fsf.UseActualSize();
        }

        private void FitScreenToolStripButton_Click(object sender, EventArgs e)
        {
            FullScreenForm fsf = (FullScreenForm)this.Owner;
            fsf.FitScreen();
        }

        internal void SlideShowEnded()
        {
            Stop_Click(this, EventArgs.Empty);
        }
    }
}